﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VgoCardChange.Models.DAL;
using VgoCardChange.Models.Objects;

namespace VgoCardChange.Models
{
    public class AdminModels
    {
        ConnectDB objData;
        #region getList Supply card
        //Danh sách nhà cung cấp thẻ
        public List<CardSupplier> GetListCardSupplier()
        {
            List<CardSupplier> lstNhaCc = new List<CardSupplier>();
            objData = new ConnectDB();
            CardSupplier nhaMang;
            string strSQL = "";
            try
            {
                strSQL = "select CardSupplierID, CardSupplierName from TblCardSupplier";
                objData.ExeReader(strSQL);
                while (objData.dataRead.Read())
                {
                    nhaMang = new CardSupplier();
                    nhaMang.CardSupplierID = objData.dataRead["CardSupplierID"].ToString();
                    nhaMang.CardSupplierName = objData.dataRead["CardSupplierName"].ToString();
                    lstNhaCc.Add(nhaMang);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                objData.CloseData();
                objData = null;
            }
            return lstNhaCc;
        }
        #endregion
        #region AdminLogin Function
        //function check user Login
        public bool CheckAdminLogin(string UserName, string PassWord)
        {
            bool isHaveUser = false;
            ConnectDB objData = new ConnectDB();
            User user;
            string strSQL = "";
            try
            {
                strSQL = "sp_checkAdminLogin @UserName = N'" + UserName + "'"
                                        + ", @PassWord ='" + PassWord + "'";
                objData.ExeReader(strSQL);
                while (objData.dataRead.Read())
                {
                    user = new User();
                    user.UserName = objData.dataRead["UserName"].ToString();
                    user.PassWord = objData.dataRead["PassWord"].ToString();
                    string role = objData.dataRead["RoleID"].ToString();
                    if (role == "")
                    {
                        role = "1";
                    }
                    int temp = Convert.ToInt32(role);
                    user.RoleID = temp;
                    if (user.UserName == UserName && user.PassWord == PassWord && user.RoleID == 1)
                    {
                        isHaveUser = true;
                        return isHaveUser;
                    }
                    else
                        isHaveUser = false;
                }
            }
            catch (Exception ex)
            {
                Console.Write("Error: " + ex);
            }
            finally
            {
                objData.CloseData();
                objData = null;
            }
            return isHaveUser;
        }
        #endregion
        #region saveDiscount
        public void SaveDisCount(Discount discount) 
        {
            objData = new ConnectDB();
            string strSQL = "";
            try
            {
                strSQL = "insert into TblDiscount(DiscountTitle, StartDate, EndDate,"
                        + "DiscountValue,[Description], UserID, CardSupplierID) Values(N'" + discount.DiscountTitle + "'" + ",'" + discount.StartDate + "'" +
                        ",'" + discount.EndDate + "'" + "," + discount.DiscountValue + ",N'" + discount.Description + "'" + "," + discount.UserID +
                        ",N'" + discount.CardSupplierID.ToString().Trim() +"')";

                objData.ExeNonQuery(strSQL);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                objData.CloseData();
                objData = null;
            }
        }
        #endregion
        //Function to get UserID(by UserName param)
        #region getUserID
        public int GetUserID(string UserName) 
        {
            objData = new ConnectDB();
            int UserID = 0;
            string strSQL = "";
            try 
            {
                strSQL = "Select UserID from TblUser where UserName=N'" + UserName +"'";
                objData.ExeReader(strSQL);
                if(objData.dataRead.Read())
                {
                    UserID = int.Parse(objData.dataRead["UserID"].ToString());
                    return UserID;
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }finally
            {
                objData.CloseData();
                objData = null;
            }
            return UserID;
        }
        #endregion

    }
}